# _*_ coding:utf-8 _*_

#封装接口get/post方法
import requests
from utils import readConfig

readConfigObj = readConfig.ReadConfig("\\config\\env.ini")

class ConfigHttp:
    def __init__(self):
        global host, port
        host = readConfigObj.getConfig("HTTP","url")
        port = readConfigObj.getConfig("HTTP","port")
        self.headers = {"Content-Type":"application/json","accept-language":"DEFULT","bitun_test_no_sign":"1","bitun_test_no_encrypt":"1"}
        self.params = {}
        self.data = {}
        self.url = None
        self.files = {}

    def set_url(self, url):
        self.url = host +":"+ port + url

    def set_headers(self, header):
        self.headers = header

    def set_params(self, param):
        self.params = param

    def set_data(self, data):
        self.data = data

    def set_files(self, file):
        self.files = file

    # defined http get method
    def get(self):
        try:
            response = requests.get(self.url, params=self.params, headers=self.headers)
            # response.raise_for_status()
            return response
        except TimeoutError:
            print("Time out!")
            return None

    # defined http post method
    def post(self):
        try:
            response = requests.post(self.url, headers=self.headers, data=self.data)
            # response.raise_for_status()
            return response.json()
        except TimeoutError:
            print("Time out!")
            return None
